/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jiadakeji.ppm.modules.business.service;

import com.jiadakeji.ppm.api.common.JsonResult;
import com.jiadakeji.ppm.common.coderule.CodeRuleFeeReceive;
import com.jiadakeji.ppm.common.coderule.GetCode;
import com.jiadakeji.ppm.common.coderule.GetCodeByRule;
import com.jiadakeji.ppm.common.persistence.Page;
import com.jiadakeji.ppm.common.service.CrudService;
import com.jiadakeji.ppm.common.utils.HttpUtils;
import com.jiadakeji.ppm.common.utils.WechatUtils;
import com.jiadakeji.ppm.modules.business.dao.FeeChargeDao;
import com.jiadakeji.ppm.modules.business.dao.FeeReceiveDao;
import com.jiadakeji.ppm.modules.business.entity.Customer;
import com.jiadakeji.ppm.modules.business.entity.FeeCharge;
import com.jiadakeji.ppm.modules.business.entity.FeeReceive;
import com.jiadakeji.ppm.modules.sys.utils.UserUtils;
import com.jiadakeji.ppm.modules.weixin.entity.WechatUnifiedorderRequest;
import com.jiadakeji.ppm.modules.weixin.entity.WechatUnifiedorderResponse;
import com.jiadakeji.ppm.modules.weixin.entity.WechatUnifiedorderRetrun;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 实收台账Service
 * @author HHAOYU
 * @version 2019-07-09
 */
@Service
@Transactional(readOnly = true)
public class FeeReceiveService extends CrudService<FeeReceiveDao, FeeReceive> {
	@Autowired
	private FeeChargeDao feeChargeDao;

	@Override
	public FeeReceive get(String id) {
		return super.get(id);
	}

	@Override
	public List<FeeReceive> findList(FeeReceive feeReceive) {
		return super.findList(feeReceive);
	}

	@Override
	public Page<FeeReceive> findPage(Page<FeeReceive> page, FeeReceive feeReceive) {
		return super.findPage(page, feeReceive);
	}



	@Transactional(readOnly = false ,rollbackFor = Exception.class)
	public void insertFeeRecive(FeeCharge feeCharge) {

	}

	@Override
	@Transactional(readOnly = false)
	public void save(FeeReceive feeReceive) {
		super.save(feeReceive);
	}


	@Override
	@Transactional(readOnly = false)
	public void delete(FeeReceive feeReceive) {
		//TODO 删除其它关联表

		super.delete(feeReceive);
	}

	@Transactional(readOnly = false,rollbackFor = Exception.class)
	public JsonResult<WechatUnifiedorderRetrun>  payByWechat(FeeReceive feeReceive, Customer customer, HttpServletRequest request){
		JsonResult<WechatUnifiedorderRetrun> result = new JsonResult<>();

		String clientIp = HttpUtils.getRealId(request);
		/*String clientIp ="123.12.12.123";*/
		WechatUnifiedorderRequest wechatRequest = new WechatUnifiedorderRequest();
		wechatRequest.setUnifiedorder(customer.getOpenid(),feeReceive,clientIp);
		wechatRequest.setNotify_url(WechatUtils.feeReceiveNotifyUrl);

		/**
		 * 生成请求微信接口的 xml格式和签名
		 */
		String xml = WechatUtils.generateRequestXml(wechatRequest);
		logger.info("微信统一下下单请求参数"+xml);


		//3.请求微信统一订单接口 返回响应对象
		String xmlResponse = WechatUtils.unifiedorder(xml);
		logger.info("微信统一下单响应:"+xmlResponse);
		// 微信响应 xml转换为object
		WechatUnifiedorderResponse wechatResponse = WechatUtils.responseXmlToObject(xmlResponse);

		//4. 判断响应结果
		if(wechatResponse.getReturn_code().equals("FAIL")){
			result.setCode(5);
			result.setMessage(wechatResponse.getReturn_msg());
			return result;

		}
		if(wechatResponse.getResult_code().equals("FAIL")){

			result.setCode(6);
			/**
			 * 错误描述
			 */
			result.setMessage(wechatResponse.getErr_code_des());
			return result;
		}
		logger.info("请求成功");

		feeReceive.preInsert();
		feeReceive.setStatus(FeeReceive.STATUS_WAIT_PAY);
		dao.insert(feeReceive);

		//5.如果可以支付,组合数据再次签名
		WechatUnifiedorderRetrun wechatUnifiedorderRetrun = generateSign(wechatResponse);

		result.setData(wechatUnifiedorderRetrun);
		return result;
	}

	private  WechatUnifiedorderRetrun generateSign(WechatUnifiedorderResponse response){

		WechatUnifiedorderRetrun wechatUnifiedorderRetrun = new WechatUnifiedorderRetrun(response);
		wechatUnifiedorderRetrun = wechatUnifiedorderRetrun.getReturnSign(wechatUnifiedorderRetrun);

		return wechatUnifiedorderRetrun;
	}

	@Transactional(readOnly = false,rollbackFor = Exception.class)
	public void payFeeChargeCash(FeeReceive feeReceive){
		FeeCharge feeCharge = feeReceive.getFeeCharge();
		GetCode getCode = new GetCodeByRule(new CodeRuleFeeReceive());
		feeReceive.setCode(getCode.generateCode());
		feeReceive.setPayAmount(feeCharge.getFeeReceive().getPayAmount());
		feeReceive.setTotalFee(feeCharge.getTotalFee());
		feeReceive.setType(feeCharge.getFeeReceive().getType());
		feeReceive.setFeeChargeId(feeCharge.getId());
		feeReceive.setCustomerId(feeCharge.getFeeReceive().getCustomerId());
		feeReceive.setFeebackMoney(feeCharge.getFeebackMoney());
		feeReceive.setCashier(UserUtils.getUser().getId());
		feeReceive.setStatus(FeeReceive.STATUS_PAY_SUCCESS);
		save(feeReceive);

		feeCharge.setStatus(FeeCharge.STATUS_FINISH);
		feeChargeDao.update(feeCharge);

	}


}
